import sys
import os

###########################################################################
### Read the nucleotides into a variable named seq
###
# You need to specify a file name
if len(sys.argv) < 2:
    print("You must supply a file name as an argument when running this program.")
    sys.exit(2)
# The file name specified on the command line, as a string.
filename1 = sys.argv[1]
filename2 = sys.argv[2]

file1_list = os.listdir(filename1)
file1_list.insert(0,filename1)
file2_list = os.listdir(filename2)
file2_list.insert(0,filename2)
###########################################################################
###Extracting subtitle text and Cleaning subtitle text
###
def Extracting_subtitle_text_and_Cleaning_subtitle_text(file_list):
    seq = ''
    i = 1
    while i < len(file_list):
        #print(os.path.join(file_list[0],file_list[i]))
        inputfile = open(os.path.join(file_list[0],file_list[i]),'r')
        for line in inputfile:
            line = line.rstrip()
            if line.isdigit() is True or '-->' in line or line is None:
                continue
            else:
                line = line.replace('<i>','')
                line = line.replace('</i>','')
                line = line.replace('<font color=#00FF00>','')
                line = line.replace('<font color=#00FFFF>','')
                line = line.replace('<font color="#00ff00">','')
                line = line.replace('<font color="#ff0000">','')
                line = line.replace('</font>','')
                line = line.replace('#','')
                line = line.replace('-','')
                line = line.replace('(','')
                line = line.replace(')','')
                line = line.replace('\xe2\x99\xaa','')
                line = line.replace('www.AllSubs.org','')
                line = line.replace('http://DJJ.HOME.SAPO.PT/','')
                line = line.replace('Downloaded','')
                line = line.replace('Shared','')
                line = line.replace('Sync','')
                line = line.replace('www.addic7ed.com', '')
                line = line.replace('n17t01','')
                line = line.replace('"','')
                line = line.replace('.',' ')
                line = line.replace('!',' ')
                line = line.replace('?',' ')
                line = line.replace('...',' ')
                line = line.replace(',', ' ')
                seq = seq + line
        seq.join(' | ')
        i += 1
    return seq

seq1 = Extracting_subtitle_text_and_Cleaning_subtitle_text(file1_list)
seq2 = Extracting_subtitle_text_and_Cleaning_subtitle_text(file2_list)

###########################################################################
###Comparing vocabulary (single words/1-grams)
###
def Comparing_vocabulary(str1,str2):
    test_list = str1.split()
    i = 0
    test_dict = {}
    while i < len(test_list):
        if test_list[i] not in str2:
            if test_list[i] in test_dict:
                test_dict[test_list[i]] += 1
            else:
                test_dict[test_list[i]] = 1
        i += 1
    test_dict = sorted(test_dict.items(),key = lambda x:x[0])
    test_dict = sorted(test_dict,key = lambda x:x[1],reverse=True)
    i = 0
    while i < 50:
        test_dict[i] = list(test_dict[i])
        test_dict[i][0] = (test_dict[i][0],)
        test_dict[i] = tuple(test_dict[i])
        print(test_dict[i])
        i += 1

def Comparing_vocabulary_augment(str1,str2):  #str1:test-corpus str2:authentic-corpus
    test_list1 = str1.split()
    test_list2 = str2.split()
    test_list_same = list(set(test_list1) & set(test_list2))

    i = 0
    test_dict_common = {}
    while i < len(test_list_same):
        num_test_corpus = str1.count(test_list_same[i])/len(test_list1)
        num_authentic_corpus = str2.count(test_list_same[i])/len(test_list2)
        test_dict_common[test_list_same[i]] = num_test_corpus/num_authentic_corpus
        i += 1
    test_dict_common = sorted(test_dict_common.items(),key = lambda x:x[0])
    test_dict_common = sorted(test_dict_common,key = lambda x:x[1],reverse=True)
    i = 0
    sum_ratio_first_50 = 0
    sum_ratio_last_50 = 0
    print("1-gram Ratios")
    print('Largest ratios:')
    while i < 50:
        test_dict_common[i] = list(test_dict_common[i])
        test_dict_common[i][0] = (test_dict_common[i][0],)
        sum_ratio_first_50 += test_dict_common[i][1]
        test_dict_common[i] = tuple(test_dict_common[i])
        print(test_dict_common[i])
        i += 1
    print(f"Sum:{sum_ratio_first_50}")
    print()
    print("Smallest ratios:")
    i = len(test_dict_common) - 1 - 49
    while i < len(test_dict_common):
        test_dict_common[i] = list(test_dict_common[i])
        test_dict_common[i][0] = (test_dict_common[i][0],)
        sum_ratio_last_50 += test_dict_common[i][1]
        test_dict_common[i] = tuple(test_dict_common[i])
        print(test_dict_common[i])
        i += 1
    print(f"Sum:{sum_ratio_last_50}")

#Comparing_vocabulary_augment(seq2,seq1)

###########################################################################
###Comparing phrases (pairs of words/2-grams)
###
def get_bigram(str1,str2):
    test_list1 = str1.split()
    test_list2 = str2.split()
    c_bigram = []
    i = 0
    j = 1
    while i < len(test_list1) and j < len(test_list1):
        if (test_list1[i] not in test_list2 and test_list1[j] not in test_list2) or (
                test_list1[i] in test_list2 and test_list1[j] not in test_list2) or (
                test_list1[i] not in test_list2 and test_list1[j] in test_list2) or test_list1[i] == '|' or test_list1[j] == '|':
            i += 1
            j += 1
            continue
        c_bigram.append((test_list1[i], test_list1[j]))
        i += 1
        j += 1
    return c_bigram
def Comparing_phrases(str1,str2):  #str1:test-corpus str2:authentic-corpus
    c1_bigram = get_bigram(str1,str2)
    c2_bigram = get_bigram(str2,str1)

    test_corpus_dict = {}
    i = 0
    while i < len(c1_bigram):
        if c1_bigram[i] not in c2_bigram:
            if c1_bigram[i] in test_corpus_dict:
                test_corpus_dict[c1_bigram[i]] += 1
            else:
                test_corpus_dict[c1_bigram[i]] = 1
        i += 1
    test_corpus_dict = sorted(test_corpus_dict.items(),key=lambda x:x[0][0])
    test_corpus_dict = sorted(test_corpus_dict, key=lambda x: x[1],reverse=True)
    test_corpus_dict = tuple(test_corpus_dict)
    i = 0
    while i < 50:
        print(test_corpus_dict[i])
        i += 1
'''
print("2-gram Frequency: Test vs. Authentic")
Comparing_phrases(seq2,seq1)
print()
print("2-gram Frequency: Authentic vs. Test")
Comparing_phrases(seq1,seq2)
'''
def Comparing_phrases_augment(str1,str2):  #str1:test-corpus str2:authentic-corpus
    c1_bigram = get_bigram(str1, str2)
    c2_bigram = get_bigram(str2, str1)
    c_bigram_same = list(set(c1_bigram) & set(c2_bigram))

    i = 0
    test_dict_common = {}
    while i < len(c_bigram_same):
        num_test_corpus = c1_bigram.count(c_bigram_same[i]) / len(c1_bigram)
        num_authentic_corpus = c2_bigram.count(c_bigram_same[i]) / len(c2_bigram)
        test_dict_common[c_bigram_same[i]] = num_test_corpus / num_authentic_corpus
        i += 1
    test_dict_common = sorted(test_dict_common.items(), key=lambda x: x[0])
    test_dict_common = sorted(test_dict_common, key=lambda x: x[1], reverse=True)
    i = 0
    sum_ratio_first_50 = 0
    sum_ratio_last_50 = 0
    print("2-gram Ratios")
    print('Largest ratios:')
    while i < 50:
        sum_ratio_first_50 += test_dict_common[i][1]
        print(test_dict_common[i])
        i += 1
    print(f"Sum:{sum_ratio_first_50}")
    print()
    print("Smallest ratios:")
    i = len(test_dict_common) - 1 - 49
    while i < len(test_dict_common):
        sum_ratio_last_50 += test_dict_common[i][1]
        print(test_dict_common[i])
        i += 1
    print(f"Sum:{sum_ratio_last_50}")

Comparing_phrases_augment(seq2,seq1)
